package com.mhkj.base.rbac.user.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.rbac.role.entity.SystemRole;
import com.mhkj.base.rbac.role.service.MyRoleService;
import com.mhkj.base.rbac.user.entity.SystemUser;
import com.mhkj.base.rbac.user.service.SystemUserService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.util.json.JsonUtils;
import com.mics.core.system.base.controller.BaseController;

/**
 * 系统用户
 * @author mics
 * @date 2018年9月1日
 * @version 1.0
 */
@Controller
@RequestMapping("/systemUser")
public class SystemUserController extends BaseController<SystemUser>{
	@Resource
	private SystemUserService systemUserService;
	@Resource
	private MyRoleService myRoleService;
	@Resource
	private JsonHandler jsonHandler;
	
	/**
	 * 用户
	 */
	@RequestMapping(value="/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response,SystemUser systemUser, int pageIndex, int pageSize ) {
		Pagination<SystemUser> pagination = systemUserService.listByPage(systemUser,pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true,"获取数据成功", pagination, new String[]{
			"id","name","loginName","code"
		}, "id","name","loginName","code");
	}

	/**
	 * 保存
	 * 
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public ResponseMessage save(long systemRoleId,String userIds) {
		if(userIds != null){
			SystemRole systemRole = myRoleService.getById(systemRoleId);
			systemUserService.save(systemRole,userIds);
		}
		return ResponseMessage.createMessage(true, "保存成功");
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(SystemUser systemUser,String roleIds) {
		if(roleIds != null){
			systemUserService.edit(systemUser,roleIds);
		}
		return ResponseMessage.createMessage(true,"修改成功");
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value="/del")
	@ResponseBody
	public ResponseMessage del(SystemUser systemUser) {
		String message = systemUserService.del(systemUser);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 查询所有用户
	 * 
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public List<SystemUser> listAll() {
		List<SystemUser> list = systemUserService.findAll();
		return list;
	}
	
	
	/**
	 * 查询用户信息
	 */
	@RequestMapping(value="/info")
	@ResponseBody
	public void info(HttpServletResponse response,long id) {
		SystemUser systemUser = systemUserService.getById(id);
		jsonHandler.sendJsonString(response, true,"获取数据成功", systemUser, new String[]{
				"id","name","code"
			}, "id","name","code");
	}
	
	
	@RequestMapping("/listRoles")
	@ResponseBody
	public void listRoles(HttpServletResponse response,long id){
		SystemUser systemUser = systemUserService.getById(id);
		List<SystemRole> systemRoles = systemUser.getSystemRoles();
		JsonUtils.sendJsonFromList(response, true,"获取数据成功", systemRoles, new String[]{"id","roleName","name","code"},
				"id","roleName","name","code");
		}
}

